﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BasePlatformNet;
using BasePlatformCommon;

namespace L9SubSet
{
	public partial class Form_DownloadLwf : Form
	{
		private BaseCoreNet baseCoreNet = null;						// 内核引用

		private Form_MainSet parent = null;							// 父窗体引用

		private string addrMapFilePath = "";						// 布线文件路径

		private Form_SelectSubEx formSelectSubDownload = null;		// 分控选择窗体引用 - 下载布线
		private Form_SelectSub formSelectSubClear = null;			// 分控选择窗体引用 - 清除布线

		private SelectedSubInfo selectedSubInfoDownload = null;		// 分控选择信息 - 下载布线
		private SelectedSubInfo selectedSubInfoClear = null;		// 分控选择信息 - 清除布线

		private string saveAddrMapDataState = "idle";				// 下载布线操作状态
		private string clearAddrMapDataState = "idle";				// 清除布线操作状态

		// 委托定义与声明
		public delegate void UpdateOperationProgressDelegate(WriteEventArgs eArgs);
		public UpdateOperationProgressDelegate updateOperationProgressDelegate;

		private string section = "DownloadLwf";						// 多语言文件配置节名称

		private string tip = "提示";									// MessageBox 题头

		/*****************************************************************************************/

		// 构造函数
		public Form_DownloadLwf(BaseCoreNet baseCoreNetEx, Form_MainSet parent, string addrMapFilePath)
		{
			InitializeComponent();

			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;
			this.Font = new System.Drawing.Font(this.Font.Name, 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel, ((byte)(134)));

			this.baseCoreNet = baseCoreNetEx;
			this.parent = parent;
			this.addrMapFilePath = addrMapFilePath;

			updateOperationProgressDelegate = new UpdateOperationProgressDelegate(updateOperationProgress);

			// 设置本地化语言
			if (LanguageOper.available == true)
			{
				setLanguageText();
			}
		}

		// 设置本地化语言
		public void setLanguageText()
		{
			if (LanguageOper.available == false)
			{
				return;
			}

			LanguageOper.SetCurrentCulture();

			tip = LanguageOper.ReadValue("Common", "tipStr");

			// 界面控件文字
			this.Text = LanguageOper.ReadValue(section, "downloadLwf.Text");
			testSaveAddrMapCheckBox.Text = LanguageOper.ReadValue(section, "testSaveAddrMapCheckBox.Text");
			processLabel1.Text = "  " + LanguageOper.ReadValue(section, "processLabel.Text");
			processLabel2.Text = "  " + LanguageOper.ReadValue(section, "processLabel.Text");
			saveAddrMapButton.Text = LanguageOper.ReadValue(section, "saveAddrMapButton.Text.1");
			clearAddrMapButton.Text = LanguageOper.ReadValue(section, "clearAddrMapButton.Text.1");
		}

		// 窗体显示后处理
		private void Form_DownloadLwf_Shown(object sender, EventArgs e)
		{
			selectedSubInfoDownload = new SelectedSubInfo();

			formSelectSubDownload = new Form_SelectSubEx(parent, selectedSubInfoDownload, baseCoreNet, "DownloadAddrMap");
			int posX = this.Location.X + (this.Size.Width - formSelectSubDownload.Size.Width) / 2;
			int posY = this.Location.Y + (this.Size.Height - formSelectSubDownload.Size.Height) / 2;
			formSelectSubDownload.Location = new Point(posX, posY);

			selectedSubInfoClear = new SelectedSubInfo();

			formSelectSubClear = new Form_SelectSub(selectedSubInfoClear, baseCoreNet);
			posX = this.Location.X + (this.Size.Width - formSelectSubClear.Size.Width) / 2;
			posY = this.Location.Y + (this.Size.Height - formSelectSubClear.Size.Height) / 2;
			formSelectSubClear.Location = new Point(posX, posY);

			baseCoreNet.WriteSubStateUpdated += updateApplyOrTestSubSendProcess;
		}

		// 窗体关闭前处理
		private void Form_DownloadLwf_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (saveAddrMapDataState.Equals("running"))
			{
				string msg = "正在下载布线数据，请先停止下载操作";
				if (LanguageOper.available == true)
				{
					msg = LanguageOper.ReadValue(section, "msgStr.0");
				}
				
				MessageBoxEx.Show(this, msg, tip, MessageBoxButtons.OK, MessageBoxIcon.Information);
				
				e.Cancel = true;
			}

			if (clearAddrMapDataState.Equals("running"))
			{
				string msg = "正在清除布线数据，请先停止清除操作";
				if (LanguageOper.available == true)
				{
					msg = LanguageOper.ReadValue(section, "msgStr.1");
				}
				MessageBoxEx.Show(this, msg, tip, MessageBoxButtons.OK, MessageBoxIcon.Information);
				e.Cancel = true;
			}

			baseCoreNet.WriteSubStateUpdated -= updateApplyOrTestSubSendProcess;
		}

		// 下载布线/停止下载
		private void saveAddrMapButton_Click(object sender, EventArgs e)
		{
			// 下载布线
			if (saveAddrMapDataState.Equals("idle"))
			{
				if (parent.subDataIsDownloading)
				{
					string msg = "正在下发 sub 文件, 请稍后操作";
					if (LanguageOper.available == true)
					{
						msg = LanguageOper.ReadValue(section, "msgStr.2");
					}
					
					MessageBoxEx.Show(this, msg, tip, MessageBoxButtons.OK, MessageBoxIcon.None);
					
					return;
				}

				if (addrMapFilePath.Equals(""))
				{
					string msg = "当前不存在布线数据";
					if (LanguageOper.available == true)
					{
						msg = LanguageOper.ReadValue(section, "msgStr.3");
					}
					MessageBoxEx.Show(this, msg, tip, MessageBoxButtons.OK, MessageBoxIcon.None);
					return;
				}

				parent.addForm("Form_SelectSubEx", formSelectSubDownload);
				formSelectSubDownload.setLanguageText();
				DialogResult dr = formSelectSubDownload.ShowDialog();
				parent.removeForm("formSelectSubDownload");

				if (dr == DialogResult.OK)
				{
					// 设置标志位
					baseCoreNet.setPauseDownloadAddrMap(false);
					baseCoreNet.setForceStopDownloadAddrMap(false);

					bool startResult = baseCoreNet.startDownloadAddrMapSubSend(selectedSubInfoDownload, preTreatCheckBox.Checked, testSaveAddrMapCheckBox.Checked);

					if (startResult == true)
					{
						saveAddrMapDataState = "running";
						updateDownloadControlState();
					}
				}
			}
			// 强制停止下载
			else if (saveAddrMapDataState.Equals("running"))
			{
				baseCoreNet.setPauseDownloadAddrMap(true);

				string msg = "确认停止下载布线数据吗？";
				if (LanguageOper.available == true)
				{
					msg = LanguageOper.ReadValue(section, "msgStr.4");
				}
				
				DialogResult dr = MessageBoxEx.Show(this, msg, tip, MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
				
				if (dr == System.Windows.Forms.DialogResult.OK)
				{
					baseCoreNet.setForceStopDownloadAddrMap(true);
					baseCoreNet.setPauseDownloadAddrMap(false);
				}
				else
				{
					baseCoreNet.setForceStopDownloadAddrMap(false);
					baseCoreNet.setPauseDownloadAddrMap(false);
				}
			}
		}

		// 清除布线/停止清除
		private void clearAddrMapButton_Click(object sender, EventArgs e)
		{
			// 清除布线
			if (clearAddrMapDataState.Equals("idle"))
			{
				if (parent.subDataIsDownloading)
				{
					string msg = "正在下发 sub 文件, 请稍后操作";
					if (LanguageOper.available == true)
					{
						msg = LanguageOper.ReadValue(section, "msgStr.2");
					}

					MessageBoxEx.Show(this, msg, tip, MessageBoxButtons.OK, MessageBoxIcon.None);

					return;
				}

				parent.addForm("Form_SelectSub", formSelectSubClear);
				formSelectSubClear.setLanguageText();
				DialogResult dr = formSelectSubClear.ShowDialog();
				parent.removeForm("Form_SelectSub");

				if (dr == DialogResult.OK)
				{
					// 设置标志位
					baseCoreNet.setPauseClearAddrMap(false);
					baseCoreNet.setForceStopClearAddrMap(false);

					// 关闭异形 - 保存分控Flash
					bool startResult = baseCoreNet.startClearAddrMapSubSend(selectedSubInfoClear, selectedSubInfoClear.overall, testSaveAddrMapCheckBox.Checked);

					if (startResult == true)
					{
						clearAddrMapDataState = "running";
						updateClearControlState();
					}
				}
			}
			// 强制停止清除
			else if (clearAddrMapDataState.Equals("running"))
			{
				baseCoreNet.setPauseClearAddrMap(true);

				string msg = "确认停止清除布线吗？";
				if (LanguageOper.available == true)
				{
					msg = LanguageOper.ReadValue(section, "msgStr.5");
				}
				
				DialogResult dr = MessageBoxEx.Show(this, msg, tip, MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
				
				if (dr == System.Windows.Forms.DialogResult.OK)
				{
					baseCoreNet.setForceStopClearAddrMap(true);
					baseCoreNet.setPauseClearAddrMap(false);
				}
				else
				{
					baseCoreNet.setForceStopClearAddrMap(false);
					baseCoreNet.setPauseClearAddrMap(false);
				}
			}
		}

		// 更新后台操作进度
		public void updateApplyOrTestSubSendProcess(object sender, EventArgs e)
		{
			WriteEventArgs eArgs = e as WriteEventArgs;
			if (eArgs != null)
			{
				this.BeginInvoke(updateOperationProgressDelegate, eArgs);
			}
		}

		// 更新后台操作进度 - 实际方法
		private void updateOperationProgress(WriteEventArgs eArgs)
		{
			int value = eArgs.processValue;
			string operation = eArgs.operation;
			string errorMsg = eArgs.errorMsg;

			#region 当前操作 - 下载布线

			if (operation.Equals("DownloadAddrMap"))
			{
				// 下载布线操作被强制停止
				if (eArgs.total == -1)
				{
					string msg = string.Format("强制停止成功，当前已下载完成 {0} 个分控。", eArgs.current);
					if (LanguageOper.available == true)
					{
						msg = string.Format(LanguageOper.ReadValue(section, "msgStr.6"), eArgs.current);
					}

					MessageBoxEx.Show(this, msg, tip, MessageBoxButtons.OK, MessageBoxIcon.None);

					// 重置下载布线相关按钮为初始状态
					saveAddrMapDataState = "idle";
					updateDownloadControlState();

					return;
				}
				// 线程中出现错误, 恢复按钮
				else if (value == -1)
				{
					if (!errorMsg.Equals(""))
					{
						string msg = "是否继续下载？";
						if (LanguageOper.available == true)
						{
							msg = LanguageOper.ReadValue(section, "msgStr.7");
						}

						DialogResult dr = MessageBoxEx.Show(this, errorMsg + "\n" + msg, tip, MessageBoxButtons.YesNo, MessageBoxIcon.Question);

						if (dr == System.Windows.Forms.DialogResult.Yes)
						{
							baseCoreNet.setDownloadLwfBreakFlag(false);
							baseCoreNet.setDownloadLwfWaitFlag(false);
							return;
						}
						else
						{
							baseCoreNet.setDownloadLwfBreakFlag(true);
							baseCoreNet.setDownloadLwfWaitFlag(false);
						}
					}
					else
					{
						string msg = "后台操作存在错误";
						if (LanguageOper.available == true)
						{
							msg = LanguageOper.ReadValue(section, "msgStr.8");
						}

						MessageBoxEx.Show(this, msg, tip, MessageBoxButtons.OK, MessageBoxIcon.None);
					}

					// 重置下载布线相关按钮为初始状态
					saveAddrMapDataState = "idle";
					updateDownloadControlState();

					return;
				}

				if (value >= 0 && value <= 100)
				{
					saveAddrMapProgressBar.Value = value;
					saveAddrProcessLabel.Text = eArgs.current + " / " + eArgs.total;
				}

				if (value == 100)
				{
					string msg = "布线数据下载完成";
					if (LanguageOper.available == true)
					{
						msg = LanguageOper.ReadValue(section, "msgStr.9");
					}

					MessageBoxEx.Show(this, msg, tip, MessageBoxButtons.OK, MessageBoxIcon.None);

					// 重置下载布线相关按钮为初始状态
					saveAddrMapDataState = "idle";
					updateDownloadControlState();
				}

				return;
			}
			#endregion

			#region 当前操作 - 清除布线
			if (operation.Equals("ClearAddrMap"))
			{
				// 下载布线操作被强制停止
				if (eArgs.total == -1)
				{
					string msg = string.Format("强制停止成功，当前已清除 {0} 个分控。", eArgs.current);
					if (LanguageOper.available == true)
					{
						msg = string.Format(LanguageOper.ReadValue(section, "msgStr.10"), eArgs.current);
					}

					MessageBoxEx.Show(this, msg, tip, MessageBoxButtons.OK, MessageBoxIcon.None);

					// 重置清除布线相关按钮为初始状态
					clearAddrMapDataState = "idle";
					updateClearControlState();

					return;
				}
				// 线程中出现错误, 恢复按钮
				else if (value == -1)
				{
					if (!errorMsg.Equals(""))
					{
						MessageBoxEx.Show(this, errorMsg, tip, MessageBoxButtons.OK, MessageBoxIcon.None);
					}
					else
					{
						string msg = "后台操作存在错误";
						if (LanguageOper.available == true)
						{
							msg = LanguageOper.ReadValue(section, "msgStr.8");
						}

						MessageBoxEx.Show(this, msg, tip, MessageBoxButtons.OK, MessageBoxIcon.None);
					}

					// 重置清除布线相关按钮为初始状态
					clearAddrMapDataState = "idle";
					updateClearControlState();
				}

				if (value >= 0 && value <= 100)
				{
					clearAddrMapProgressBar.Value = value;
					clearAddrProcessLabel.Text = (eArgs.current + 1) + " / " + eArgs.total;
				}

				if (value == 100)
				{
					string msg = "布线数据清除完成";
					if (LanguageOper.available == true)
					{
						msg = LanguageOper.ReadValue(section, "msgStr.11");
					}

					MessageBoxEx.Show(this, msg, tip, MessageBoxButtons.OK, MessageBoxIcon.None);

					// 重置清除布线相关按钮为初始状态
					clearAddrMapDataState = "idle";
					updateClearControlState();
				}

				return;
			}
			#endregion
		}

		// 设置下载布线相关控件状态
		private void updateDownloadControlState()
		{
			if (saveAddrMapDataState.Equals("running"))
			{
				string msg = "停止下载";
				if (LanguageOper.available == true)
				{
					msg = LanguageOper.ReadValue(section, "saveAddrMapButton.Text.0");
				}
				saveAddrMapButton.Text = msg;
				clearAddrMapButton.Enabled = false;
				testSaveAddrMapCheckBox.Enabled = false;
			}
			else if (saveAddrMapDataState.Equals("idle"))
			{
				string msg = "下载布线";
				if (LanguageOper.available == true)
				{
					msg = LanguageOper.ReadValue(section, "saveAddrMapButton.Text.1");
				}
				saveAddrMapButton.Text = msg;
				clearAddrMapButton.Enabled = true;
				testSaveAddrMapCheckBox.Enabled = true;
				saveAddrMapProgressBar.Value = 0;
				saveAddrProcessLabel.Text = "0 / 0";
			}
		}

		// 重置清除布线相关按钮为初始状态
		private void updateClearControlState()
		{
			if (clearAddrMapDataState.Equals("running"))
			{
				string msg = "停止清除";
				if (LanguageOper.available == true)
				{
					msg = LanguageOper.ReadValue(section, "clearAddrMapButton.Text.0");
				}
				clearAddrMapButton.Text = msg;
				saveAddrMapButton.Enabled = false;
				testSaveAddrMapCheckBox.Enabled = false;
			}
			else if (clearAddrMapDataState.Equals("idle"))
			{
				string msg = "清除布线";
				if (LanguageOper.available == true)
				{
					msg = LanguageOper.ReadValue(section, "clearAddrMapButton.Text.1");
				}
				clearAddrMapButton.Text = msg;
				saveAddrMapButton.Enabled = true;
				testSaveAddrMapCheckBox.Enabled = true;
				clearAddrMapProgressBar.Value = 0;
				clearAddrProcessLabel.Text = "0 / 0";
			}
		}
	}
}
